﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tower : MonoBehaviour {
    public TowerSensor sensor;


    public PlayerType TowerFavor;
    public float influencePoint;
    public float MaxinfluencePoint;
    public float influenceSpeed;

    public PlayerType TowerOwner
    {
        get { return influencePoint == MaxinfluencePoint ? TowerFavor : PlayerType.None; }
    }
    

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        if (sensor.MaxUnitOwner == PlayerType.None) return;
        if (sensor.MaxUnitOwner!= TowerFavor)
        {
            influencePoint -=  influenceSpeed * sensor.ValidUnitCount * Time.deltaTime;
            if(influencePoint<0)
            {
                influencePoint = 0;
                TowerFavor = sensor.MaxUnitOwner;
            }
        }
        else
        {
            
            float sumInfluence =  influencePoint+  influenceSpeed * sensor.ValidUnitCount * Time.deltaTime;
            influencePoint = sumInfluence > MaxinfluencePoint ? MaxinfluencePoint : sumInfluence;
        }

    }
}
